package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Override
    public PageResult<Dept> list(String name, Integer status, Integer page, Integer pageSize) {
        //设置pageHelper分页参数
        /* PageHelper.startPage(page, pageSize);*/
        PageHelper.startPage(page, pageSize);

        //2.调用mapper接口方法,执行查询
        List<Dept> deptList = deptMapper.selectList(name, status);
        Page<Dept> p = (Page<Dept>) deptList;

        return  new PageResult<>(p.getTotal(),p.getResult());
    }
    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept) ;

    }

    @Override
    public void deleteDept(Integer id) {
        deptMapper.deleteById(id);

    }

    @Override
    public Dept getDeptById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update( dept);
    }

    @Override
    public List<Dept> getAllDept() {
        return deptMapper.selectDept();
    }


}
